package IO;

import javax.jws.soap.SOAPBinding;
import java.io.*;

/*
缓存的目的：
解决在写入文件操作时，频繁的操作文件所带来的性能问题
BufferedOutputStream 内部的默认缓存大小为8kb，也可以自己设置 ；
 每次写入是存储到缓存中的byte数组中，当数组存满时，会把数组中的数据写入文件同时缓存下标归0，下次写入会覆盖上次的字节

字符流：
加入字符缓存流，增强读取功能(readLine)
更高效的读取数据
FileReader：内部使用InputStreamReader(sun.nio.cs.StreamDecoder)，解码过程byte-char，默认缓存为8k
BufferedReader：默认缓存大小为8k，但是可以手动指定缓存大小，把数据进接读取到缓存中，减少每次转换过程，效率更高
 */
public class BufferED {
    public static void main(String[] args) {
          charRead();


    }
    private static void byteWrite(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            OutputStream out = new FileOutputStream(file);
            BufferedOutputStream  bos = new BufferedOutputStream(out);
            String tt = "小河流水哗啦啦";
            bos.write(tt.getBytes());
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static void byteRead(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            InputStream in = new FileInputStream(file);
            BufferedInputStream  bis = new BufferedInputStream(in);
            byte[] bytes = new byte[1024];
            int len = -1 ;
            StringBuilder sb = new StringBuilder();
            while((len=bis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));
            }
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private static void charWrite(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            Writer writer = new FileWriter(file);
            BufferedWriter writer1 = new BufferedWriter(writer);
            writer1.write("小桥流水人家");
            writer1.flush();
            writer1.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    private static void charRead(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\1207.txt");
        try {
            Reader reader = new FileReader(file);
            char[] chars = new char[1024];
            //为字符流提供缓存，以达到高效读取的功能
            BufferedReader reader1 = new BufferedReader(reader);
            int len = -1;
            while ((len = reader1.read(chars))!=-1){
                System.out.println(new String(chars,0,len));
            }
            reader1.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
